/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gprs.sever;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author kienbk1910
 */
public class ReaderSocket implements Runnable{
    private Socket rSocket;
    private MainFrame mainFrame;
    final static int USER_CONNECT  =0;
    final static int DEVICE_CONNECT=1;
    final static int USER_NAME     =2;
    final static int USER_PASSWORD =3;
    final static int USER_SELECT   =4;
    final static int USER_STATUS   =5;
    final static int USER_PROGRESS =6;
    final static int USER_STATUS_ACK=7;
    private int type;
    public ReaderSocket(Socket a, MainFrame frame){
        rSocket   =a;
        mainFrame = frame;
    }
    @Override
    public void run() {
        try {
            //throw new UnsupportedOperationException("Not supported yet.");
            BufferedReader dataReceiver = new BufferedReader(new InputStreamReader(this.rSocket.getInputStream()));
            Character data ;
            String packData="";
            boolean loop = true;
            while(loop == true ){
                data = (char)dataReceiver.read();
                if(data != null){
                  packData=packData+data;
                 // this.mainFrame.setTextArea(data.toString());
                }
                if(data.equals('>')==true){
                    if(this.extractData(packData).equals(packData)==false){
                        this.process(this.extractData(packData),this.type);
                        this.mainFrame.setTextUserArea(this.extractData(packData)+"\n");
                       // this.mainFrame.setSelectionStart();
                        packData="";
                    }
                }
            }
        } catch (IOException ex) {
            Logger.getLogger(ReaderSocket.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }
    
    public String extractData(String packet){
        this.type= -1;
        if(packet.startsWith("<USER_CONNECT>")==true && packet.endsWith("</USER_CONNECT>")==true){
            this.type=USER_CONNECT;
            packet="";
    
        }
        if(packet.startsWith("<DEVICE_CONNECT>")==true && packet.endsWith("</DEVICE_CONNECT>")==true){
            this.type=DEVICE_CONNECT;
            packet="";
    
        }
        if(packet.startsWith("<USER_NAME>")==true && packet.endsWith("</USER_NAME>")==true){
            this.type=USER_NAME;
            packet=packet.substring(11, packet.length()-12);
    
        }
        if(packet.startsWith("<USER_PASSWORD>")==true && packet.endsWith("</USER_PASSWORD>")==true){
            this.type=USER_PASSWORD;
            packet=packet.substring(15, packet.length()-16);
    
        }
        if(packet.startsWith("<USER_SELECT>")==true && packet.endsWith("</USER_SELECT>")==true){
            this.type=USER_SELECT;
            packet=packet.substring(13,packet.length()-14);
    
        }
        if(packet.startsWith("<USER_STATUS>")==true && packet.endsWith("</USER_STATUS>")==true){
            this.type=USER_STATUS;
            packet=packet.substring(13, packet.length()-14);
    
        }
        if(packet.startsWith("<USER_PROGRESS>")==true && packet.endsWith("</USER_PROGRESS>")==true){
            this.type=USER_PROGRESS;
            packet=packet.substring(15,packet.length()-16);
    
        }
        if(packet.startsWith("<USER_STATUS_ACK>")==true && packet.endsWith("</USER_STATUS_ACK>")== true){
               this.type= USER_STATUS_ACK;
                       packet="";
        }
          return packet;
    }
    public void process(String st, int type) throws IOException{
        switch(type){
            case USER_NAME:
                //truy xuất database
                break;
            case USER_PASSWORD:
                //truy xuât database
                break;
            case USER_SELECT:
                //truy xuat database tra gửi trong so thiet bị
                System.out.println("da vao");
                 this.sendMessages(rSocket, "OFF",USER_STATUS);
                // this.sendMessages(rSocket,"50",USER_PROGRESS);
                break;
            case USER_STATUS_ACK:
                 this.sendMessages(rSocket, "50", USER_PROGRESS);
                break;
        }
        
    
    }
    public String packMessages(String mgs,int type){
		String out="";
		switch(type){
		    case USER_CONNECT:
		    	out="<USER_CONNECT>"+mgs+"</USER_CONNECT>";
		    	break;
		    case 1:
		    	out="<DEVICE_CONNECT>"+mgs+"</DEVICE_CONNECT>";
		    	break;
		    case USER_NAME:
		    	out = "<USER_NAME>"+mgs+"</USER_NAME>";
		    	break;
		    case USER_PASSWORD:
		    	out = "<USER_PASSWORD>"+mgs+"</USER_PASSWORD>";
		    	break;
		    case USER_SELECT:
		    	out = "<USER_SELECT>"+mgs+"</USER_SELECT>";
		    	break;
		    case USER_STATUS:
		    	out = "<USER_STATUS>"+mgs+"</USER_STATUS>";
		    	break;
		    case USER_PROGRESS:
		    	out = "<USER_PROGRESS>"+mgs+"</USER_PROGRESS>";
		    	break;   
                    case USER_STATUS_ACK:
                        out= "<USER_STATUS_ACK>"+mgs+"</USER_STATUS_ACK>";
                        break;
		
		}
		return out;
    }
                        
   public void sendMessages(Socket socket,String mgs,int type) throws IOException{
		OutputStreamWriter out= new OutputStreamWriter(socket.getOutputStream());
		out.write(packMessages(mgs,type));
                System.out.println(packMessages(mgs,type));
		out.flush(); 
  }
}
